package com.gdue.commons;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.fasterxml.jackson.databind.deser.std.StringArrayDeserializer;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 *  公共字段填充，避免代码重复书写
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpSession session;

    /**
     * 执行insert操作时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("createUser",session.getAttribute("employee"));
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",session.getAttribute("employee"));

    }

    /**
     * 执行update操作时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",session.getAttribute("employee"));

    }
}
